package com.ordertracker.entity;

import com.ordertracker.constant.GetByName;
import com.ordertracker.util.Preconditions;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Description of class.
 * Created 1/18/13 1:28 PM
 *
 * @author andrey.rodin@playtech.com
 */

@XmlRootElement
public class Role extends VersionedEntity {
    private static final long serialVersionUID = 535952697707531140L;

    private String name;
    private Type type;

    public Role() {
    }

    public Role(Long id, Long version, String name, Type type) {
        setId(id);
        setVersion(version);
        this.name = name;
        this.type = type;
    }

    public enum Type implements GetByName<Type>{
        ADMIN("ADMIN"),
        MANAGER("MANAGER"),
        CLIENT("CLIENT");

        private Type(String name) {
            this.name = name;
        }

        private String name;

        public String getName() {
            return name;
        }

        public Type getByName(String name) {
            Preconditions.<String>checkNull(name, "RoleType: parameter=name must be not null!");
            for (Type type : Type.values()) {
                if (type.getName().equals(name))
                    return type;
            }
            return null;
        }
    }

    @XmlElement(nillable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement(nillable = false)
    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Role)) return false;
        if (!super.equals(o)) return false;

        Role role = (Role) o;

        if (!name.equals(role.name)) return false;
        if (type != role.type) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + type.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "Role{" +
                "name='" + name + '\'' +
                ", type=" + type.getName() +
                ", " + super.toString();
    }
}
